// pages/Search/index.js

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:'../../images/',
    historyList: [],     // 历史记录
    hotList: ['新开河桥', '赵家沟桥', '西新港桥',],    // 热门推荐
    search_value: ''   , // 搜索关键词
    check_value:'',
    selectArray: [],
    bridgeList:[],
    dealList:[],
    bridgeIdList:[]

  },
  /**
   * 生命周期函数--监听页面加载
   */
   onLoad: function (options) {
    this.getBridgeDate()
    this.getMaskData()
    if (wx.getStorageSync('historyList')) {
      this.setData({
        historyList: wx.getStorageSync('historyList')
      })
    }

  },
  //获取桥梁数据
  getBridgeDate(){
    this.setData({
      bridgeList:app.globalData.bridgeList
    })
  },
  //获取标段接口数据
  getMaskData:function(){
    this.setData({
      selectArray:app.globalData.maskDataList
    })
    console.log(this.data.selectArray)
  },
 
   //搜索关键词监听
   inputValue(e) {
    this.setData({
      search_value: e.detail.value,  
    })
    
  },
  select:function(e){
    console.log(e)
    this.setData({
      search_value:e.detail.code,
    })
  },
  //点击键盘上搜索按钮 去搜索页面
  confirm() {
    this.go_result();
  },
  //点击搜索，去搜索页面
  go_result() {
    if(this.data.search_value!=''){
      this.go_result_deal()
    }else{
    wx.showToast({
      title:'内容不能为空',
      icon:'error'
    })
  }
  },
  go_result_deal:function(){
    let arr = this.data.historyList
      if (!arr.includes(this.data.search_value)) {
        arr.push(this.data.search_value)
        this.setData({
        historyList: arr
      })
      wx.setStorageSync('historyList', this.data.historyList)
    }
    this.dealData()
    app.globalData.dealList=this.data.dealList
    console.log(app.globalData.dealList)
    wx.reLaunch({
      url: '/pages/codeimage/index'
    })
  },
  //点击删除按钮
  del() {
    this.setData({
      historyList: []
    })
    wx.removeStorageSync('historyList')
  },
  // 点击历史记录和热门推荐
  goSearch(e) {
    this.setData({
      search_value:e.currentTarget.dataset.val
    })
    let arr = this.data.historyList
    if (!arr.includes(e.currentTarget.dataset.val)) {
      arr.push(e.currentTarget.dataset.val)
      this.setData({
        historyList: arr,
      })
      wx.setStorageSync('historyList', this.data.historyList)
    }
  },
  //根据搜索内容筛选数据，然后渲染
  dealData:function(){
    var val=this.data.search_value
    for(var index in this.data.bridgeList){
      if(this.data.bridgeList[index].bridgeid==val||this.data.bridgeList[index].bridgename==val||this.data.bridgeList[index].section==val){
        this.data.dealList.push(this.data.bridgeList[index])
      }else if(val===''){
        this.data.dealList.push(app.globalData.bridgeList)
      }
    }
  },
})